<?php
// +----------------------------------------------------------------------
// | 作品管理
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;

class TryItemsController extends AdminBase {

    //当前栏目id
    public $cateid = 0;

    public $tryid=0;
    //模型缓存
    protected $model = array();

    //初始化模型
    protected function _initialize() {
        parent::_initialize();
        $this->cateid = I('request.cateid', $_POST['cateid'], 'intval');
        $this->tryid = I('request.tryid', $_POST['tryid'], 'intval');

    }

    //首页
    public function index() {
        if (IS_POST) {
            $this->redirect('index', $_POST);
        }
        //查询条件
        $where = array();

        //搜索
        $search = I('get.search');
        if (!empty($search)) {
            $this->assign("search", $search);
            //添加开始时间
            $start_time = I('get.start_time');
            if (!empty($start_time)) {
                $start_time = strtotime($start_time);
                $where['create_time'] = array("EGT", $start_time);
                $this->assign('start_time', $start_time);
            }
            //添加结束时间
            $end_time = I('get.end_time');
            if (!empty($end_time)) {
                $end_time = strtotime($end_time);
                $where['create_time'] = array("ELT", $end_time);
                $this->assign('end_time', $end_time);
            }
            if ($end_time > 0 && $start_time > 0) {
                $where['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time));
            }
            //搜索字段
            $searchtype = I('get.searchtype', null, 'intval');
            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("searchtype", $searchtype);
                $this->assign("keyword", $keyword);
                if ($searchtype ==1) {
                    $where['title'] = array("LIKE", "%{$keyword}%");
                }elseif ($searchtype==2) {
                    $userid=M('TryMember')->where(array('mname'=>$keyword))->getField('id');
                    $where['memberid']=array('EQ',(int) $userid);
                }
            }
            //状态
            $status = I('get.status', 0, 'intval');
            if ($status==1) {
                $where['status'] = array("EQ", true);
            }elseif($status==2){
                $where['status']=array('EQ',false);
            }
            $this->assign('status',$status);
        }
        $model=M('TryItems');
        //信息总数
        $count = $model->where($where)->count();
        $page = $this->page($count, 30);
        $data = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("iftj"=>"DESC","sort" => "DESC",'tryid'=>'DESC'))->select();
        $this->assign('Page', $page->show())
            ->assign('cateid', $this->cateid)
            ->assign('count', $count)
            ->assign('data', $data);
        $this->display();
    }


    //添加信息
    public function add() {
        if (IS_POST) {
            //栏目ID
            $cateid = intval($_POST['cateid']);
            $post=$_POST;
            //获取当前栏目配置
            $model=D('Admins/TryItems');

            $status = $model->addModel($post);
            if ($status) {
                $this->success("商品添加成功！");
            } else {
                $error = $model->getError();
                $this->error($error ? $error : '添加失败！');
            }

        } else {
                $this->assign("category", D('Admins/TryCategory')->getTryCategory());
                $this->assign("member", $this->getMemberList());
                $this->assign('start_time',time());
                $this->assign('end_time',time()+3600*24);
                $this->display();
        }
    }

    /**
     * 编辑作品
     */
    public function edit(){
        if (IS_POST) {
            $data=$_POST;
            $model=D('Admins/TryItems');
            $status = $model->editModel($data);
            if ($status) {
                $this->success("商品更新成功！");
            } else {
                $error = $model->getError();
                $this->error($error ? $error : '更新失败！');
            }

        } else {
            if (empty($this->tryid)) {
                $this->error('该作品不存在');
            }
            $data=D('Admins/TryItems')->find($this->tryid);
            $this->assign('data',$data);
            $this->assign("member", $this->getMemberList());
            $this->assign("category", D('Admins/TryCategory')->getTryCategory());
            $this->display();

        }
    }



    //删除产品
    public function delete(){
        $model=D('Admins/TryItems');
        $tryid=I('get.tryid','','intval');
        if($model->deleteModel($tryid)){
            $this->success('成功删除商品');
        }else{
            $error=$model->getError();
            $this->error($error ? $error : '删除商品失败');
        }
    }


    //审核单个产品
    public function pass(){
        $workid=I('get.workid');
        if(empty($itemid)){
            $this->error('审核失败');
        }
        $where['workid']=$workid;
        if(M('Works')->where($where)->setField(array('status'=>true))){
            $this->success('审核成功');
        }else{
            $this->error('审核失败');
        }
    }


    //批量处理信息(审核,取消审核,删除)
    public function batch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>true);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>false);
                    $msg='取消审核';
                    break;
                case 'checktj':
                    $data=array('iftj'=>true);
                    $msg='推荐';
                    break;
                case 'canceltj':
                    $data=array('iftj'=>false);
                    $msg='取消推荐';
                    break;
                case 'checkhot':
                    $data=array('ifhot'=>true);
                    $msg='热门';
                    break;
                case 'cancelhot':
                    $data=array('ifhot'=>false);
                    $msg='取消热门';
                    break;
            }
            foreach ($ids as $id) {
                M('TryItems')->where(array('tryid'=>$id))->save($data);
            }
            $this->success($msg.'成功！');
        }
    }

    //批量排序
    public function batch_sort(){
        $ids = $_POST['ids'];
        if (!$ids) {
            $this->error('没有信息被选中！');
        }
        $Items = M('TryItems');
        foreach ($_POST['sort'] as $id => $sort) {
            $Items->where(array('tryid' => $id))->save(array('sort' => $sort));
        }
        $this->success("排序更新成功！");
    }

    //批量删除
    public function batch_delete(){
        $ids = $_POST['ids'];
        if (!$ids) {
            $this->error('没有信息被选中！');
        }
        foreach($ids as $id){
                D('Admins/TryItems')->deleteModel($id);
            }
        $this->success('成功删除作品');
    }


    //获取商家
    public function getMemberList(){
        return M('TryMember')->field('id,mname,mtel')->where(array('status'=>true))->select();
    }



    /*
     *试用申请列表
     */
    public function applylist() {
        if (IS_POST) {
            $this->redirect('applylist', $_POST);
        }
        //查询条件
        $where = array();
        //状态
        $status = I('get.status');
        if(isset($_GET['status']) && $_GET['status']==0){
            $where['status']=false;
        }elseif(isset($_GET['status'])){
            $where['status']=$status;
        }
        $this->assign('statuss', $status);

        //搜索
        $search = I('get.search');
        if (!empty($search)) {
            $this->assign("search", $search);
            //添加开始时间
            $start_time = I('get.start_time');
            if (!empty($start_time)) {
                $start_time = strtotime($start_time);
                $where['create_time'] = array("EGT", $start_time);
                $this->assign('start_time', $start_time);
            }
            //添加结束时间
            $end_time = I('get.end_time');
            if (!empty($end_time)) {
                $end_time = strtotime($end_time);
                $where['create_time'] = array("ELT", $end_time);
                $this->assign('end_time', $end_time);
            }
            if ($end_time > 0 && $start_time > 0) {
                $where['create_time'] = array(array('EGT', $start_time), array('ELT', $end_time));
            }

            //搜索字段
            $searchtype = I('get.searchtype', null, 'intval');
            //搜索关键字
            $keyword = \Input::getVar(I('get.keyword'));
            if (!empty($keyword)) {
                $this->assign("searchtype", $searchtype);
                $this->assign("keyword", $keyword);
                if ($searchtype ==1) {
                    $wheres['title'] = array("EQ", $keyword);
                    $tryid=M('TryItems')->where($wheres)->getField('tryid');
                    $where['tryid']=array('EQ',$tryid);
                }elseif ($searchtype==2) {
                    $userid=M('User')->where(array('nickname'=>$keyword))->getField('userid');
                    $where['userid']=array('EQ',(int) $userid);
                }
            }

        }
        $gets=I('get.');
        if(!empty($gets['uid'])){
            $where['userid']=$gets['uid'];
        }
        if(!empty($gets['tryid'])){
            $where['tryid']=$gets['tryid'];
        }

        $model=M('TryApply');
        //信息总数
        $count = $model->where($where)->count();
        $page = $this->page($count, 30);
        $data = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id"=>"DESC"))->select();
        foreach($data as $k=>$v){
            $data[$k]['reportid']=M('TryReport')->where(array('tryid'=>$v['tryid']))->getField('id');
        }
        $this->assign('Page', $page->show())
            ->assign('count', $count)
            ->assign('data', $data);
        $this->display();
    }

    //批量处理信息(审核,取消审核,删除)
    public function apply_batch() {
        if (IS_POST) {
            $ids = $_POST['ids'];
            if (!$ids) {
                $this->error('没有信息被选中！');
            }
            $doAct=I('get.do');
            switch($doAct){
                case 'checkpass':
                    $data=array('status'=>1);
                    $msg='审核';
                    break;
                case 'cancelpass':
                    $data=array('status'=>0);
                    $msg='取消审核';
                    break;
                case 'nopass':
                    $data=array('status'=>2);
                    $msg='不通过审核';
                    break;
            }
            foreach ($ids as $id) {
                $defautstatus=M('TryApply')->where(array('id'=>$id))->getField('status');

                M('TryApply')->where(array('id'=>$id))->save($data);
                if($doAct=='checkpass' && $defautstatus!=true){
                    $datas['applynum'] = array('exp','applynum+1');
                    $datas['lastnum'] = array('exp','lastnum-1');
                    $udata['trynum'] = array('exp' , 'trynum+1');
                }elseif($doAct=='cancelpass' || $doAct=='nopass' && $defautstatus==true){
                    $datas['applynum'] = array('exp','applynum-1');
                    $datas['lastnum'] = array('exp','lastnum+1');
                    $udata['trynum'] = array('exp' , 'trynum-1');
                }
                $try=M('TryApply')->field('userid,tryid')->where(array('id'=>$id))->find();
                M('TryItems')->where(array('tryid'=>$try['tryid']))->save($datas);
                M('User')->where(array('userid'=>$try['userid']))->save($udata);
            }
            $this->success($msg.'成功！');
        }
    }


    //批量删除
    public function apply_batch_delete(){
        $ids = $_POST['ids'];
        if (!$ids) {
            $this->error('没有信息被选中！');
        }
        foreach($ids as $id){
            $result=M('Admins/TryApply')->delete($id);
            if($result){
                $data['applynum'] = array('exp','applynum-1');
                $data['lastnum'] = array('exp','lastnum+1');
                $udata['trynum'] = array('exp' , 'trynum-1');
                $try=M('TryApply')->field('userid,tryid')->where(array('id'=>$id))->find();
                M('TryItems')->where(array('tryid'=>$try['tryid']))->save($data);
                M('User')->where(array('userid'=>$try['userid']))->save($udata);
            }else{
                $this->error('删除失败');
            }
        }
        $this->success('成功删除信息');
    }

    //删除
    public function apply_delete(){
        $model=M('TryApply');
        $id=I('get.id','','intval');
        if($model->delete($id)){
            $data['applynum'] = array('exp','applynum-1');
            $data['lastnum'] = array('exp','lastnum+1');
            $udata['trynum'] = array('exp' , 'trynum-1');
            $try=M('TryApply')->field('userid,tryid')->where(array('id'=>$id))->find();
            M('TryItems')->where(array('tryid'=>$try['tryid']))->save($data);
            M('User')->where(array('userid'=>$try['userid']))->save($udata);
            $this->success('成功删除');
        }else{
            $this->error('删除失败');
        }
    }

    //根据手机号查询商家
    public function checktel(){
        //搜索关键字
        $keyword = \Input::getVar(I('get.shtxt'));
        if(empty($keyword)){
            exit(json_encode(array('status'=>false)));
        }
        $where['mtel'] = array("LIKE", "%{$keyword}%");
        $list=M('TryMember')->field('mname,mtel,id')->where($where)->select();
        if($list){
            exit(json_encode(array('status'=>true,'datas'=>$list)));
        }else{
            exit(json_encode(array('status'=>false)));
        }

    }




}
